package cn.e4j.modular.api.service;


import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.SpringContextHolder;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat100Mapper;
import cn.e4j.modular.api.mapper.OuterUserMapper;
import cn.e4j.modular.common.mapper.SysAuthorityMapper;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.mapper.SystemMapper;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class OuterUserService {

    @Autowired
    OuterUserMapper outerUserMapper;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    SysAuthorityMapper sysAuthorityMapper;

    @Autowired
    GwbService gwbService;

    public List<Dto> getGoodsInfo(Page page,Dto indto){

        return outerUserMapper.getGoodsInfo(page,indto);
    }

    public Dto getAuthority(SysUser user, Dto dto) {
        Dto gwb001 = gwbService.getGwb001(user, dto);//客户权限

        dto.put("ORGS",systemService.getSys003(user).get("ORGS"));//获取部门权限
        dto.put("GWAES",this.getGwaesAuthority(user,dto));//获取渠道权限
        dto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));//客户权限
        return dto;
    }

    public List<Dto> getProducts(Dto indto){
        return outerUserMapper.getProductList(indto);
    }

    public List<Dto> getPromoteProducts(Dto indto){
        return InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.OuterUserMapper.getPromoteProducts",indto);
    }



    private List<Dto> getGwaesAuthority(SysUser user, Dto dto){
        List<Dto> dtoList = null;
        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", user.getDBC());
        queryDto.put("ID", user.getID());
        queryDto.put("GWAA001", user.getACCOUNT());
        Dto gwb001 = sysAuthorityMapper.getGwb001(dto);
        if (gwb001.getInteger("HYCOUNT") > 0) {
            dtoList = sysAuthorityMapper.getGwae(queryDto);
            if (ToolUtil.isEmpty(dtoList)) {
                dtoList = new ArrayList<>();
                Dto defaultDto = Dtos.newDto();
                defaultDto.put("ID", "GWAE001");
                dtoList.add(defaultDto);
            }
        }
        return dtoList;
    }


    public Dto getCount(SysUser user, Dto inDto) {
        inDto.put("MOBILE",user.getMOBILE());
        inDto.put("GWAA001",user.getACCOUNT());
        inDto.put("MMRDENT",user.getDBC());
        int orderCount = outerUserMapper.getOrderCount(inDto);
        int serverCount = outerUserMapper.getServerCount(inDto);
        List<Dto> commentCount = outerUserMapper.getCommentCount(inDto);

        Dto result = new Dto();
        result.put("orderCount",orderCount);
        result.put("serverCount",serverCount);
        result.put("commentCount",commentCount.get(0).getBigDecimal("COUNT").add(commentCount.get(1).getBigDecimal("COUNT")));
        return result;
    }

    public List<Dto> getUnfinished(Dto inDto) {
        return outerUserMapper.getUnfinishedServer(inDto);
    }
}
